<%--
  Created by IntelliJ IDEA.
  User: DELL
  Date: 2024/4/22
  Time: 16:19
  To change this template use File | Settings | File Templates.
--%>
<%@ page isELIgnored="false" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>博客列表</title>
    <link rel="stylesheet" href="/static/layui/css/layui.css">
    <script src="/static/jquery-2.1.4.js" type="text/javascript" charset="utf-8"></script>
    <script src="/static/layui/layui.js" type="text/javascript" charset="utf-8"></script>
    <script src="/static/mylayer.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
    <!--顶部：搜索form表单-->
    <form class="layui-form">
        学生姓名：
        <div class="layui-inline layui-input-wrap">
            <input type="text" name="name" lay-affix="clear" class="layui-input">
        </div>
        所在系：
        <div class="layui-inline layui-input-wrap">
            <select id="xiId" name="xiId" lay-filter="xiId">
                <option value="">请选择</option>
            </select>
        </div>


        <div class="layui-inline">
            <button class="layui-btn" lay-submit lay-filter="submitSearch">搜索</button>
            <button type="reset" class="layui-btn layui-btn-primary">清空</button>
        </div>
    </form>

    <!--页内容-->
    <table class="layui-hide" id="test" lay-filter="test"></table>

    <%--右侧：触发单元格工具事件---单个元素的删除、编辑--%>
    <script type="text/html" id="barDemo">
        <div class="layui-clear-space">
            <%--<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete">删除</a>--%>
            <a class="layui-btn layui-btn-xs" lay-event="update">分数</a>
        </div>
    </script>



    <script>
        /*layui库中表单模块form，表格模块table,日期选择器模块laydate*/
        layui.use(['form','table'],function (){
            var form = layui.form;
            var table = layui.table;

            form.on('switch(statusFilter)',function (obj) {
                var id = this.value;
                var checked = obj.elem.checked;
                console.log(checked);
                var status = checked ? 1 : 0;
                $.post(
                    '/teacherStu?method=updateStatus',
                    {'id':id,'status':status},
                    function (result) {
                        if (result.code == 0){
                            mylayer.okMsg(result.msg);
                        }
                    },
                    'json'
                );
            });


            $.post(
                '/xi?method=selectAll',
                function (result) {
                    if (result.code == 0){
                        $(result.data).each(function () {
                            $('#xiId').append('<option value="'+this.id+'">'+this.name+'</option>');
                        })
                        //局部变动，渲染一下
                        form.render('select');
                    }
                },
                'json'
            );

            // 创建渲染实例
            table.render({
                elem:'#test', /*页内容的id*/
                id:'tableId',
                url:'/teacherStu?method=selectByPage',
                toolbar: '#toolbarDemo',
                cols:[[
                    {field:'id',title:'ID',sort:true,width: 80},/*sort用于排序*/
                    {field: 'name',title: '学生姓名'},
                    {field: 'age',title: '年龄'},
                    {field: 'gender',title: '性别',escape:false},/*escape:false处理内容中的代码*/
                    /*{field: 'status',title: '状态',templet:'#statusTemplet',width: 100},*/
                    /*{field: 'clickCount',title: '点击数量',width: 100},*/
                    {field: 'courseId',title: '课程id'},
                    {field: 'courseName',title: '课程名'},
                    {field: 'scores',title: '选修课分数'},
                    {field: 'gredit',title: '学分'},
                    {field: 'xiName',title: '所在系'},
                    {field: 'clickCount',title: '修改成绩的次数'},
                    /*{field: 'createTime',title: '创建时间',width: 170},*/
                    {fixed:'right',title: '操作',width:100,toolbar:'#barDemo'}
                ]],
                page: true
            });

            //搜索提交
            form.on('submit(submitSearch)',function (data) {
                //获得表单字段
                var field = data.field;
                //执行搜索重载
                table.reload('tableId',{
                    page: {
                        //重新从第1页开始
                        curr:1
                    },
                    //搜索的字段
                    where:field
                });
                //阻止默认 form跳转
                return false;
            });


            //触发单元格工具事件---单个元素的删除、编辑
            table.on('tool(test)',function (obj) {
                //获得当前行数据
                var data = obj.data;
                console.log(obj);
                console.log(obj.data);
                if (obj.event === 'update'){
                    var index = layer.open({
                        type:2,
                        title:'编辑数据',
                        area:['60%','60%'],// 宽高
                        content:'/page/teacherstu/update?studentId=' + data.id + '&courseId=' + data.courseId,
                    });
                }
            });
        });
    </script>
</body>
</html>
